﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shop.Core.Domain.Common
{
    [Serializable]
    public class Mass
    {
        public Mass(decimal value = 0, MassUnit unit = MassUnit.G, int precision = 2, bool isAutoRounding = true)
        {
            this.Value = value;
            this.Unit = unit;
            this.Precision = precision;
            this.IsAutoRounding = isAutoRounding;
        }
        public MassUnit Unit
        {
            get;
            protected set;
        }

        public decimal Value
        {
            get;
            protected set;
        }
        /// <summary>
        /// 获取精度设置
        /// </summary>
        public int Precision
        {
            get;
            protected set;
        }
        /// <summary>
        /// 获取是否自动Round
        /// </summary>
        public bool IsAutoRounding
        {
            get;
            protected set;
        }

        //#region op

        //public static Mass operator +(Mass mass, float value)
        //{
        //    float amount = mass.Value + value;
        //    return new Mass(amount, mass.Unit, mass.IsAutoRounding, mass.Precision, mass.IsAutoRounding);
        //}

        //public static Mass operator +(Mass mass1, Mass mass2)
        //{
        //    //CheckParamHasSameCurrency(money1, money2);
        //    CheckParamHasSameRounding(money1, money2);
        //    CheckParamHasSameAutoRounding(money1, money2);
        //    decimal amount = money1.value + money2.value;
        //    return new Money(amount, money1.isAutoRounding,
        //        money1.rounding, Math.Max(money1.precision, money2.precision));
        //}

        //public static Money operator -(Money money, decimal value)
        //{
        //    decimal sumVal = money.value - value;
        //    return new Money(sumVal, money.isAutoRounding,
        //        money.rounding, money.precision);
        //}

        //public static Money operator -(Money money1, Money money2)
        //{
        //    //CheckParamHasSameCurrency(money1, money2);
        //    CheckParamHasSameRounding(money1, money2);
        //    CheckParamHasSameAutoRounding(money1, money2);
        //    decimal sumVal = money1.value - money2.value;
        //    return new Money(sumVal, money1.isAutoRounding,
        //        money1.rounding, Math.Max(money1.precision, money2.precision));
        //}

        //public static Money operator *(Money money, decimal times)
        //{
        //    decimal sumVal = money.value * times;
        //    var result = new Money(sumVal, money.isAutoRounding,
        //        money.rounding, money.precision);
        //    result.Round();
        //    return result;
        //}

        //public static Money operator /(Money money, decimal divider)
        //{
        //    decimal resultVal = money.value / divider;
        //    var result = new Money(resultVal, money.isAutoRounding,
        //        money.rounding, money.precision);
        //    result.Round();
        //    return result;
        //}

        //public static bool operator >(Money money1, Money money2)
        //{
        //    //CheckParamHasSameCurrency(money1, money2);
        //    return money1.value > money2.value;
        //}

        //public static bool operator >=(Money money1, Money money2)
        //{
        //    return !(money1 < money2);
        //}

        //public static bool operator <(Money money1, Money money2)
        //{
        //    //CheckParamHasSameCurrency(money1, money2);
        //    return money1.value < money2.value;
        //}

        //public static bool operator <=(Money money1, Money money2)
        //{
        //    return !(money1 > money2);
        //}

        //public static bool operator ==(Money money1, Money money2)
        //{
        //    return money1.value == money2.value;
        //}

        //public static bool operator !=(Money money1, Money money2)
        //{
        //    return money1.value != money2.value;
        //}

        //#endregion

        //#region private helper

        ///// <summary>
        ///// 检查两个Money是否具有相同的四舍五入方式。若不是则抛出异常。
        ///// </summary>
        ///// <param name="money1">第一个Money</param>
        ///// <param name="money2">第二个Money</param>
        //static void CheckParamHasSameRounding(Money money1, Money money2)
        //{
        //    if (!money1.HasSameRounding(money2))
        //    {
        //        throw new ArgumentException(Resources.Money_RoundingMustIdentical);
        //    }
        //}
        ///// <summary>
        ///// 检查两个Money是否具有相同的自动舍入设置。若不是则抛出异常。
        ///// </summary>
        ///// <param name="money1">第一个Money</param>
        ///// <param name="money2">还是Money</param>
        //static void CheckParamHasSameAutoRounding(Money money1, Money money2)
        //{
        //    if (!money1.HasSameAutoRounding(money2))
        //    {
        //        throw new ArgumentException(Resources.Money_AutoRoundingMustIdentical);
        //    }
        //}

        //#endregion
    }
    [Serializable]
    public enum MassUnit
    {
        /// <summary>
        /// 克
        /// </summary>
        G = 0,
        /// <summary>
        /// 千克
        /// </summary>
        KG = 1,
    }
}
